package com.lemon.components.ant.project.makeservice.beanparser;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.lemon.components.ant.util.ConfigUtil;
import com.lemon.components.ant.util.PathUtil;
import com.lemon.ui.codemaker.component.RecentlyFile;

import lombok.Data;

public class PackageChoicer {
	
	@Data
	public static class PackageInfo {
		private String packageName;
		private String packagePath;
	}

	public static void main(String[] args) {
		PackageChoicer packageChoicer = new PackageChoicer ();
		packageChoicer.packageList("biz");
	}
	
	public void folders(File folderFile,List<PackageInfo> packages) {
		if (folderFile.isDirectory()) {
			PackageInfo packageInfo = new PackageInfo();
			packageInfo.setPackagePath(folderFile.getAbsolutePath());
			if (packages.size() > 0) {
				String subfolder = packages.get(0).getPackagePath();
				String tempString = StringUtils.remove(folderFile.getAbsolutePath(), subfolder);
				packageInfo.setPackageName(tempString.substring(1, tempString.length()).replace("\\", "."));
			}
			packages.add(packageInfo);
			for (File tmp : folderFile.listFiles()) {
				folders(tmp, packages);
			}
		}
	}
	
	public List<PackageInfo> packageList(String modelName){
		List<PackageInfo> packages = new ArrayList<>();
		folders(PathUtil.getInstance(ConfigUtil.getInstance(RecentlyFile.configFilePath).getProjectInfo()).getJavaSourcePath(modelName).getFile(),packages);
		if (packages.size() > 0) {
			packages.remove(0);
		}
		return packages;
	}
}
